<template>
  <!-- 用户页顶部条 -->
  <div style="position: relative;left: 100px">
  <div style="height: 6vh;background-color:#42b983;padding:10px 20px;width: 1100px">
    <span style="line-height:60px;font-size: 20px;">考试详情</span>
    <el-button @click="gotoExternalPage" color="#fff"
               style="float:right;margin-top:13px;color: #42b983;">论坛讨论</el-button>

  </div>
  <el-card style="margin: 20px;height: 70px;background-color:#e1f3d8 ;width: 1100px ">
    <el-form :inline="true">
      <el-form-item label="类目名称">
        <el-input placeholder="请输入类目名称"
                  style="width: 220px;" v-model="searchCategoryForm.category" @keydown.enter="loadCategory"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button @click="resetSearchCategory">重置</el-button>
        <el-button color="#42b983" style="color:#fff;" @click="loadCategory">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:20px; background-color: #e1f3d8 ;width: 1100px;  ">
    <el-table :data="categoryArr" style="background-color: #e1f3d8">
      <el-table-column prop="category" label="类目名称" width="100px" align="center "></el-table-column>

      <el-table-column prop="categoryContent"  width="500px" label="类目概述" align="center"></el-table-column>
      <el-table-column prop="url" label="url" width="300px" align="center "></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"
                     :disabled="scope.row.level==40" @click="resetPassword(scope.row.url)">一键跳转官网</el-button>
          <el-button link type="primary" @click="detail(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 用户查询卡片 -->
  <el-card style="margin: 20px;height: 70px;background-color:#e1f3d8 ;width: 1100px ">
    <el-form :inline="true">
      <el-form-item label="考试名称">
        <el-input placeholder="考试名称"
                  style="width: 220px;" v-model="searchUserForm.examName" @keydown.enter="loadUser"></el-input>
      </el-form-item>

      <el-form-item >
        <el-button @click="resetSearch">重置</el-button>
        <el-button color="#42b983" style="color: #f4f7f6" @click="loadUser">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 显示用户信息表格 -->
  <el-card style="margin:20px;background-color: #e1f3d8;width: 1100px">
    <el-table :data="userArr">
      <el-table-column prop="examsId" label="考试id" width="80" align="center" ></el-table-column>
      <el-table-column prop="examName" label="考试名称" align="center"></el-table-column>
      <el-table-column prop="examDate1" label="考试时间" align="center"></el-table-column>
      <el-table-column prop="registrationStart" label="报名开始时间" align="center"></el-table-column>
      <el-table-column prop="registrationStart" label="报名结束时间" align="center"></el-table-column>
    </el-table>
  </el-card>
  </div>
<!--  用户详情弹窗-->
  <el-dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form label-position="top">
      <el-form-item label="介绍">
        <span>{{detailForm.categoryContent}}</span>
      </el-form-item>
    </el-form>
    <el-form label-position="讨论区">
      <el-form-item>
        <a href="{{detailForm.url}}"><el-button>讨论区</el-button></a>
      </el-form-item>
    </el-form>
  </el-dialog>


</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";


// 这里不需要 ref，因为我们不绑定到模板的响应式数据
const gotoExternalPage = () => {
  window.location.href = 'https://www.baidu.com';
};



const dialogVisible = ref(false);
const detailForm = ref({
  categoryContent:'',
  url:'',
  category:''
})

const detail = ()=>{
  dialogVisible.value = true;
}



let id = localStorage.categoryId
const categoryArr=ref([]);
const loadArr = ()=>{
  axios.get(BASE_URL+'/v1/user/select1?id='+id).then((response) =>{
    if (response.data.code==2000){
      categoryArr.value = response.data.data;
      detailForm.value = response.data.data[0];
      console.log(detailForm.value)
    }
  })
}
onMounted(()=>{
  loadArr()
})

const dialogTitle = ref('详情介绍')
//定义数组用来装返回的类目概述

const searchCategoryForm = ref({category:''})
const loadCategory = ()=>{
  let data = qs.stringify(searchCategoryForm.value);
  axios.get(BASE_URL+'/v1/user/select1?'+data).then((response)=>{
    if(response.data.code==2000) {
      categoryArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
//页面一加载立即请求加载用户数据

const resetSearchCategory = () => {
  searchCategoryForm.value = {};//清空用户搜索表单数据
  loadCategory();//重新加载用户数据
}
const resetPassword = (url) => {

  window.location.href = url;
}

//定义数组用来装用户列表数据
const userArr = ref([]);
const loadFormArr = ()=>{
  let categoryId = id;
  axios.get(BASE_URL+'/v1/user/select2?categoryId='+categoryId).then((response)=>{
    if (response.data.code==2000){
      userArr.value = response.data.data;
    }
  })
}
onMounted(()=>{
  loadFormArr();
})
//定义对象保存查询条件
const searchUserForm = ref({examName: ''})
//定义加载用户的方法
const loadUser = () => {
  //有条件则根据条件查员工数据,没有条件默认查所有员工数据
  let data = qs.stringify(searchUserForm.value);
  axios.get(BASE_URL + '/v1/user/select2?' + data).then((response) => {
    if (response.data.code == 2000) {
      userArr.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
//页面一加载立即请求加载用户数据
//重置用户搜索信息
const resetSearch = () => {
  searchUserForm.value = {};//清空用户搜索表单数据
  loadUser();//重新加载用户数据
}


</script>

<style scoped>
el-card {
  position: absolute;
  top: 50%; /* 将卡片向上移动其自身高度的50% */
  left: 50%; /* 将卡片向左移动其自身宽度的50% */
  transform: translate(-50%, -50%); /* 使用转换来修正偏移量，实现居中 */
}

</style>